package com.example.customeview;

import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Handler;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * @author Caleb_F
 * @description: 带双手缩放
 * @date :2020-09-21 22:03
 */
public class PhotoViewTest extends AppCompatActivity {

        private ImageView myImageview;
        private GestureDetector gestureDetector;
        private Matrix myMatrix = new Matrix();
        private Matrix savedMatrix = new Matrix();
        private static final String TAG = "PictureActivity";
        private static final int NONE = 0;
        private static final int DRAG = 1;
        private static final int ZOOM = 2;
        private static final int DOUBLE_TA = 2;
        private int mode = NONE;
        private PointF startPoint = new PointF();
        private PointF middlePoint = new PointF();
        private float oridis = 1f;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_picture);
            initView();
        }

        /**
         * 初始化控件
         */
        private void initView() {
            myImageview = (ImageView) findViewById(R.id.my_imageview);
            myImageview.setOnTouchListener(new MyTouchListener());
            gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
                private Matrix doubleTapMatirx = new Matrix();
                @Override
                public boolean onDoubleTap(MotionEvent e) {

                    doubleTapMatirx.postScale(2f, 2f, e.getX(), e.getY());
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            myImageview.setImageMatrix(doubleTapMatirx);
                        }
                    }, 200);
                    return true;
                }
                @Override
                public boolean onSingleTapConfirmed(MotionEvent e) {

                    return true;
                }
            });

        }

        //计算两个触碰点之间的距离
        private float distance(MotionEvent event) {
            float x = event.getX(0) - event.getX(1);
            float y = event.getY(0) - event.getY(1);
            return (float) Math.sqrt(x * x + y * y);
        }

        private PointF middle(MotionEvent event) {
            float x = event.getX(0) + event.getX(1);
            float y = event.getY(0) + event.getY(1);
            return new PointF(x / 2, y / 2);

        }

        class MyTouchListener implements View.OnTouchListener {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                ImageView view = (ImageView) v;
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        myMatrix.set(view.getImageMatrix());
                        savedMatrix.set(myMatrix);
                        startPoint.set(event.getX(), event.getY());
                        mode = DRAG;
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        oridis = distance(event);
                        if (oridis > 10f) {
                            savedMatrix.set(myMatrix);
                            middlePoint = middle(event);
                            mode = ZOOM;
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_POINTER_UP:
                        //单击直接恢复
                        mode = NONE;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (mode == DRAG) {
                            myMatrix.set(savedMatrix);
                            myMatrix.postTranslate(event.getX() - startPoint.x, event.getY() - startPoint.y);
                        } else if (mode == ZOOM) {
                            float distance = distance(event);
                            if (distance > 10f) {
                                myMatrix.set(savedMatrix);
                                float scale = distance / oridis;
                                myMatrix.postScale(scale, scale, middlePoint.x, middlePoint.y);
                            }
                        }
                        break;
                    default:
                        break;
                }
                view.setImageMatrix(myMatrix);
                return true;
            }

        }

    }

